package com.gx.smart.construction.ui.common.viewmodel;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.blankj.utilcode.util.ToastUtils;
import com.casic.gx.grpc.common.ComQuery;
import com.casic.gx.grpc.common.ComResp;
import com.gx.smart.base.livedata.SingleLiveEvent;
import com.gx.smart.lib.event.LoadingMessageEvent;
import com.gx.smart.lib.http.api.ConstructionAppOrganizationalStructureProviderService;
import com.gx.smart.lib.http.api.asynctask.CallBack;
import com.gx.wisestone.constructionappgrpclib.grpc.organizationalstructure.ConstructionAppOrganizationalStructureDto;
import com.gx.wisestone.constructionappgrpclib.grpc.organizationalstructure.ConstructionAppOrganizationalStructureQueryResponse;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * @author: xiaosy
 * 时间: 2022/2/10
 * @Description: java类作用描述
 * @Version: 1.0
 **/
public class CommonViewModel extends SelectRoomViewModel {
    private MutableLiveData<List<ConstructionAppOrganizationalStructureDto>> areaList;
    private MutableLiveData<List<ConstructionAppOrganizationalStructureDto>> buildList;
    private MutableLiveData<List<ConstructionAppOrganizationalStructureDto>> unitList;
    private String areaId;
    private String buildingId;

    public CommonViewModel(@NonNull Application application) {
        super(application);
    }

    public LiveData<List<ConstructionAppOrganizationalStructureDto>> getAreaList(int currentPage) {
        EventBus.getDefault().post(new LoadingMessageEvent(true));
        if (areaList == null) {
            areaList = new MutableLiveData<>();
            queryAreaList(currentPage);
        }else {
            areaList.setValue(areaList.getValue());
        }
        EventBus.getDefault().post(new LoadingMessageEvent(false));
        return areaList;
    }

    private void queryAreaList(int currentPage) {
        ComQuery comQuery = ComQuery
                .newBuilder()
                .setPageSize(20)
                .setPage(currentPage)
                .build();
        ConstructionAppOrganizationalStructureProviderService.getInstance().queryOrganizationalStructure(comQuery, "", 1, new CallBack<ConstructionAppOrganizationalStructureQueryResponse>() {
            @Override
            public void callBack(ConstructionAppOrganizationalStructureQueryResponse result) {
                if (result == null) {
                    error.setValue(true);
                    ToastUtils.showLong("服务器超时");
                    return;
                }

                ComResp comResp = result.getComResp();
                if (comResp.getCode() == 100) {
                    areaList.setValue(result.getContentList());
                } else {
                    error.setValue(true);
                    handleServiceException(comResp);
                }
            }
        });
    }


    public LiveData<List<ConstructionAppOrganizationalStructureDto>> getBuildList(int currentPage, String parentId) {
        EventBus.getDefault().post(new LoadingMessageEvent(true));
        if (buildList == null) {
            buildList = new SingleLiveEvent<>();
        }
        if (TextUtils.equals(parentId, areaId)) {
            buildList.setValue(buildList.getValue());
        } else {
            loadBuildList(currentPage, parentId);
        }
        this.areaId = parentId;
        EventBus.getDefault().post(new LoadingMessageEvent(false));
        return buildList;
    }

    private void loadBuildList(int currentPage, String parentId) {
        ComQuery comQuery = ComQuery
                .newBuilder()
                .setPageSize(50)
                .setPage(currentPage)
                .build();
        ConstructionAppOrganizationalStructureProviderService.getInstance().queryOrganizationalStructure(comQuery, parentId, 2, new CallBack<ConstructionAppOrganizationalStructureQueryResponse>() {

            @Override
            public void callBack(ConstructionAppOrganizationalStructureQueryResponse result) {
                if (result == null) {
                    error.setValue(true);
                    ToastUtils.showLong("服务器超时");
                    return;
                }

                ComResp comResp = result.getComResp();
                if (comResp.getCode() == 100) {
                    buildList.setValue(result.getContentList());
                } else {
                    error.setValue(true);
                    handleServiceException(comResp);
                }
            }
        });
    }


    public LiveData<List<ConstructionAppOrganizationalStructureDto>> getUnitList(int currentPage, String parentId) {
        EventBus.getDefault().post(new LoadingMessageEvent(true));
        if (unitList == null) {
            unitList = new SingleLiveEvent<>();
        }
        if (TextUtils.equals(parentId, buildingId)) {
            unitList.setValue(unitList.getValue());
        } else {
            loadUnitList(currentPage, parentId);
        }
        this.buildingId = parentId;
        EventBus.getDefault().post(new LoadingMessageEvent(false));
        return unitList;
    }

    private void loadUnitList(int currentPage, String parentId) {
        ComQuery comQuery = ComQuery
                .newBuilder()
                .setPageSize(20)
                .setPage(currentPage)
                .build();
        ConstructionAppOrganizationalStructureProviderService.getInstance().queryOrganizationalStructure(comQuery, parentId, 3, new CallBack<ConstructionAppOrganizationalStructureQueryResponse>() {

            @Override
            public void callBack(ConstructionAppOrganizationalStructureQueryResponse result) {
                if (result == null) {
                    error.setValue(true);
                    ToastUtils.showLong("服务器超时");
                    return;
                }

                ComResp comResp = result.getComResp();
                if (comResp.getCode() == 100) {
                    unitList.setValue(result.getContentList());
                } else {
                    error.setValue(true);
                    handleServiceException(comResp);
                }
            }
        });
    }

}
